<?php


namespace apps\marriage\app\mp\controller;


use apps\marriage\core\model\MemberProfile;
use apps\marriage\core\model\ServiceChargeOrder;
use apps\marriage\core\service\ActivityService;
use apps\marriage\core\service\MemberProfileService;
use apps\marriage\core\service\ServiceChargeOrderService;
use apps\marriage\core\service\SiteConfigService;
use think\Exception;
use think\Request;

class ServiceChargeOrderController extends BaseMarriageMpController
{
    /**
     * @var ServiceChargeOrderService
     */
    private $serviceChargeOrderService;

    /**
     * @var SiteConfigService
     */
    private $siteConfigService;

    /**
     * @var ActivityService
     */
    private $activityService;

    /**
     * @var MemberProfileService
     */
    private $memberProfileService;

    protected function init()
    {
        parent::init();
        $this->serviceChargeOrderService = service('ServiceChargeOrder', SERVICE_NAMESPACE);
        $this->siteConfigService = service('SiteConfig', SERVICE_NAMESPACE);
        $this->activityService = service('Activity', SERVICE_NAMESPACE);
        $this->memberProfileService = service('MemberProfile', SERVICE_NAMESPACE);
    }

    public function create(Request $request)
    {
        $fromuid = $request->param('fromuid', 0, 'intval');
        $iid = $request->param('iid', 0, 'intval');
        $iidType = $request->param('iid_type', 0, 'intval');
        $moneyType = $request->param('money_type', 'money');

        if($fromuid == 0) {
            $fromuid = $this->user['fromuid'];
        } else if($fromuid > 0 && $fromuid == $this->user['id']) {
            $fromuid = 0;
        }

        $orderMoney = 0.00;

        $prices = $this->siteConfigService->getValueByCode('prices');

        if($iidType == ServiceChargeOrder::IID_TYPE_UNLOCK_MEMBER_PHONE) {
            $rights = $this->memberProfileService->getVipRightsByUserId($this->user['id']);
            $orderMoney = $rights['unlock_phone_price'];
        } else if($iidType == ServiceChargeOrder::IID_TYPE_UNLOCK_MEMBER_WXID) {
            $rights = $this->memberProfileService->getVipRightsByUserId($this->user['id']);
            $orderMoney = $rights['unlock_wxid_price'];
        } else if($iidType == ServiceChargeOrder::IID_TYPE_FACEID_DETECT) {
            $orderMoney = floatval($prices['faceid_detect']);
        } else if($iidType == ServiceChargeOrder::IID_TYPE_ACTIVITY) {
            $activity = $this->activityService->getByPk($iid);
            $memberProfile = $this->memberProfileService->getByUserId($this->user['id']);
            if($memberProfile['gender'] == MemberProfile::GENDER_MALE) {
                $orderMoney = floatval($activity['charge_male']);
            } else if($memberProfile['gender'] == MemberProfile::GENDER_FEMALE) {
                $orderMoney = floatval($activity['charge_female']);
            }
        } else if($iidType == ServiceChargeOrder::IID_TYPE_UNLOCK_CHAT_SESSION) {
            $orderMoney = floatval($prices['unlock_chat_session']);
        }

        $order = [
            'user_id' => $this->user['id'],
            'fromuid' => $fromuid,
            'iid' => $iid,
            'iid_type' => $iidType,
            'order_money' => $orderMoney
        ];

        if($moneyType == 'money') {
            $order['money_type'] = ServiceChargeOrder::MONEY_TYPE_CNY;
            $orderSn = $this->serviceChargeOrderService->createOrder($order);
        } else if($moneyType == 'gold_coin') {
            $order['money_type'] = ServiceChargeOrder::MONEY_TYPE_GOLD_COIN;
            $orderSn = $this->serviceChargeOrderService->createOrderByUseGoldCoin($order);
        }
        return $this->success(['order_sn' => $orderSn]);
    }
}